(function(IGrid) {
	// 国际化信息
	var menuMsg = IGrid.Const.I18n.menu = {
		btnDelete : "ccay.common.button.gridDelete",
		btnUndoDelete : "ccay.common.button.gridUndoDelete",
		btnCopy : "ccay.common.button.copy"
	};

	var contextMenu = {
		/** @description 设置列的编辑器配置
		 *  @param {Object} grid IGrid对象
		 *  @param {Object} col 列的配置信息
		 */
		colSet : function(grid, col) {
			col.menuable && (col.cssName += " "+grid.id + "-menuable");
		},
		/** @description 初始化菜单
		 *  @param {Object} grid IGrid对象
		 *  @param {Object} menuOp 菜单的配置信息
		 */
		initMenus : function(grid, op) {
			var menuOp = op.menuOp,headMenuOp = op.headMenuOp;
			if(menuOp)
			{
				menuOp.fnGetData = function(cell) {
					return grid.getRowData(cell);
				}
				// 设置菜单
				menuOp.menus && this.setMenus(menuOp.menus);
				// 绑定菜单
				this.bindMenu(grid, menuOp,grid.getBodyPanel());
			}
			if(headMenuOp)
			{
				headMenuOp.setBorder = false;
				this.bindMenu(grid, headMenuOp,grid.getHeadPanel());
			}
		},
		/** @description 设置菜单
		 *  @param {Object} menus 菜单对象
		 */
		setMenus : function(menus) {
			// 遍历菜单
			for (var i = 0, len = menus.length; i < len; i++) {
				var menu = menus[i];
				if (menu.type) {
					// 通过菜单类型获取菜单
					var innerMenu = InnerMenus.get(menu.type);
					if (innerMenu) {
						if ($.isArray(innerMenu)) {
							$.each(innerMenu, function() {
										menus[i] = $.extend(menu, innerMenu);
									})
						} else
							menus[i] = $.extend(menu, innerMenu);
						delete menu.type;
					} else {
						menus.splice(i--, 1);
					}
				}
			}
		},
		/** @description 绑定菜单
		 *  @param {Object} grid IGrid对象
		 *  @param {Object} menuOp 菜单的配置信息
		 */
		bindMenu : function(grid, menuOp,panel) {
			// 设置菜单的目标对象
			!menuOp.target && (menuOp.target = grid.id + "-menuable");
			menuOp.offset = "fixed";
			//menuOp.fixed = true;
			panel.contextMenu(menuOp);
		}
	};

	// 创建内置菜单对象
	var InnerMenus = $.IAdapter();

	InnerMenus.add("del", {
				// 内置标记为删除按钮
				text : $i18n(menuMsg.btnDelete),
				css : "delete",
				click : function(cell, menu) {
					// 获取gird对象
					var grid = IGrid.Helper.getGridObj(cell);
					IGrid.Operation.del.delRow(grid.getCellRow(cell), true, grid);
				},
				fnCheck : function(cell, menu, data) {
					return data && !data._deleted;
				}
			})
			.add("undo", {
				// 内置取消删除按钮
				text : $i18n(menuMsg.btnUndoDelete),
				css : "undo",
				click : function(cell, menu) {
					var grid = IGrid.Helper.getGridObj(cell);
					IGrid.Operation.del.delRow(grid.getCellRow(cell), false, grid);
				},
				fnCheck : function(cell, menu, data) {
					return data && data._deleted;
				}
			}).add("copy", {
				// 内置复制按钮
				text : $i18n(menuMsg.btnCopy),
				css : "copy",
				click : function(cell, menu) {
					var grid = IGrid.Helper.getGridObj(cell), row = grid.getCellRow(cell), record = grid.getRowData(row), op = grid.op, optOp = op.operation.insert, setting = {};
					setting.posTr = row;
					IGrid.Operation.insert.copyInsert(grid, op, record, optOp, setting);
				}
			});

	/** 注册上下文菜单插件 */
	Ccay.UI.XControl.addPlugin("XGrid", "contextMenu", {
				build : function(grid, op) {
					// 初始化菜单列
					if (op.contextMenu) {
						var handler = op.handler;
						//表头菜单
						op.menuOp && handler.setColumn.add(contextMenu.colSet);
						handler.initComplete.add(function(grid,op) {
							contextMenu.initMenus(grid,op);
						});
					}
				},
				clear : function(grid, op) {
					// 清除上下文菜单列的样式
					if (op.columns) {
						$.each(op.columns, function(i, col) {
									if (col.menuable) {
										col.cssName = null;
									}
								});
					}
					// 清除菜单的鼠标事件
					grid.dom.unbind("mouseover").unbind("mouseleave");
				}
			});
	Ccay.UI.XControl.extendIManager("XGrid", {
		/** @description 复制单元格数据
		 * 	@param {jDom} cell 单元格对象
		 *  @param {Boolean} isVertical 是否是竖直复制
		 */
		copyCellData: function (cell, isVertical) {
			var grid = this, currCell, currCellOp,
				// 获取单元格数据
				cellOp = grid.getColumnOp(cell), cellData = grid.cellValue(cell, cellOp),
				tempTarget = isVertical ? grid.getCellRow(cell) : cell.next();
			while (tempTarget.length) {
				currCell = isVertical ? grid.getRowCell(tempTarget, cellOp._i) : tempTarget;
				currCellOp = grid.getColumnOp(currCell);
				// 设置当前单元格的值
				if (currCellOp.editable !== false) {
					grid.cellValue(currCell, currCellOp, cellData, true);
					!grid.isNewRow(currCell.parents("tr:first")) && currCell.addClass('cell-update');
				}
				tempTarget = isVertical ? tempTarget.next() : currCell.next();
			}
		}
	});

})(Ccay.UI.IGrid);